package com.fbs.wowo.userCollect.service.imp;

import com.fbs.wowo.essay.entity.Essay;
import com.fbs.wowo.essay.mapper.EssayMapper;
import com.fbs.wowo.exception.DataOverflowException;
import com.fbs.wowo.exception.ErrorCode;
import com.fbs.wowo.manage.entity.User;
import com.fbs.wowo.manage.mapper.UserMapper;
import com.fbs.wowo.message.entity.UserMsg;
import com.fbs.wowo.message.service.UserMsgService;
import com.fbs.wowo.message.util.MsgConst;
import com.fbs.wowo.userCollect.entity.UserCollect;
import com.fbs.wowo.userCollect.entity.UserCollectListResponse;
import com.fbs.wowo.userCollect.mapper.UserCollectMapper;
import com.fbs.wowo.userCollect.service.UserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("UserCollectService")
@Transactional
public class UserCollectServiceImp implements UserCollectService {

    @Resource
    private UserCollectMapper userCollectMapper;

    @Resource
    private EssayMapper essayMapper;

    @Resource
    private UserMapper userMapper;



    //添加收藏
    @Override
    public int insertSelective(UserCollect record) {
        User user = userMapper.findByUserId(record.getUserId());
        if (user!=null){
            Essay essay = essayMapper.selectByPrimaryKey(record.getEssayId());
            if (essay!=null){
                if (record.getUserId().equals(essay.getUserId())){
                    //不能收藏自己
                    throw new DataOverflowException(ErrorCode.ERR_COLLECT_ESSAY);
                }
                UserCollect userCollect = userCollectMapper.selectByPrimaryKey(record.getUserId(), record.getEssayId());
                if (userCollect==null){
                    record.setCreateDate(new Date());
                    // 发消息
                    sendMsg(essay.getUserId(), essay.getId(), essay.getTitle());
                    return userCollectMapper.insertSelective(record);
                }
                //已收藏
                throw new DataOverflowException(ErrorCode.ERR_COLLECT_TO);
            }
            //图文数据为空
            throw new DataOverflowException(ErrorCode.ERR_ESSAY_NULL);
        }
        //用户不存在
        throw new DataOverflowException(ErrorCode.ERR_USER_NOT_EXIST);
    }

    private void sendMsg(String userId, String enId, String content) {
        UserMsg userMsg = new UserMsg();
        userMsg.setUserId(userId);
        userMsg.setMsgType(MsgConst.MsgCollect);
        userMsg.setMsgContent("有新的用户收藏了你的 " + content);
        userMsg.setEntityId(enId);
        userMsg.setEntityType(MsgConst.EnEssay);
        userMsgService.add(userMsg);
    }

    //根据用户ID和作品ID查看是否已收藏
    @Override
    public UserCollect selectByPrimaryKey(String userId, String essayId) {
        return userCollectMapper.selectByPrimaryKey(userId,essayId);
    }

    //取消收藏
    @Override
    public int deleteByPrimaryKey(String userId, String essayId) {
        return userCollectMapper.deleteByPrimaryKey(userId,essayId);
    }

    //根据用户ID查询收藏列表
    @Override
    public List<UserCollectListResponse> findCollectListByUserId(String userId) {
        return userCollectMapper.findCollectListByUserId(userId);
    }

    @Autowired
    private UserMsgService userMsgService;
}
